package cm.iccsoft.android.telecomcost.ui;

import java.util.ArrayList;
import cm.iccsoft.android.telecomcost.DatabaseManager;
import cm.iccsoft.android.telecomcost.R;
import cm.iccsoft.android.telecomcost.TelecomCost;
import cm.iccsoft.android.telecomcost.activities.WeeklyThresholdListActivity;
import cm.iccsoft.android.telecomcost.entities.MonthlyThreshold;
import cm.iccsoft.android.telecomcost.entities.WeeklyThreshold;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class WeeklyThresholdDialog extends Dialog {

	private DatabaseManager db;
	
	private EditText txtWeek1;
	private EditText txtWeek2;
	private EditText txtWeek3;
	private EditText txtWeek4;
	
	private Button btnClose;
	private Button btnSave;
	
	private boolean shouldRefreshThresholdList = false;
	
	private Context _context;	

	/**
	 * @param context
	 * @param cancelable
	 * @param cancelListener
	 */
	public WeeklyThresholdDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		_context = context;
	}

	/**
	 * @param context
	 * @param theme
	 */
	public WeeklyThresholdDialog(Context context, int theme) {
		super(context, theme);
		_context = context;
	}

	public WeeklyThresholdDialog(Context context) {
		super(context);
		_context = context;
	}

	private void saveWeeklyThreshold() throws Exception {		
		Log.i("WeeklyThresholdDialog", "saving WeeklyThreshold...");		
		
		WeeklyThresholdListActivity activity = (WeeklyThresholdListActivity) _context;

		Log.i("WeeklyThresholdDialog", "we load the WeeklyThreshold first...");
		int currentMonth = activity.getMonthlyThreshold().getMonth().getMonthOfYear();
		ArrayList<WeeklyThreshold> thresholds = db.getWeeklyThreshold(currentMonth);
		
		WeeklyThreshold threshold = db.getWeeklyThreshold(MonthlyThreshold.PROP_WEEK1,currentMonth ,
				thresholds);
		threshold.setAmount(txtWeek1.getText().toString().equals("") ? 0 : Double.parseDouble(txtWeek1.getText().toString()));		
		MonthlyThreshold thres = db.saveWeeklyThreshold(threshold,activity.getMonthlyThreshold());
		activity.setMonthlyThresgold(thres);
		
		threshold = db.getWeeklyThreshold(MonthlyThreshold.PROP_WEEK2,currentMonth,
				thresholds);
		threshold.setAmount(txtWeek2.getText().toString().equals("") ? 0 : Double.parseDouble(txtWeek2.getText().toString()));		
		thres = db.saveWeeklyThreshold(threshold,activity.getMonthlyThreshold());
		activity.setMonthlyThresgold(thres);
		
		threshold = db.getWeeklyThreshold(MonthlyThreshold.PROP_WEEK3,currentMonth,
				thresholds);
		threshold.setAmount(txtWeek3.getText().toString().equals("") ? 0 : Double.parseDouble(txtWeek3.getText().toString()));		
		thres = db.saveWeeklyThreshold(threshold,activity.getMonthlyThreshold());
		activity.setMonthlyThresgold(thres);
		
		threshold = db.getWeeklyThreshold(MonthlyThreshold.PROP_WEEK4,currentMonth,
				thresholds);								
		threshold.setAmount(txtWeek4.getText().toString().equals("") ? 0 : Double.parseDouble(txtWeek4.getText().toString()));		
		thres = db.saveWeeklyThreshold(threshold,activity.getMonthlyThreshold());
		activity.setMonthlyThresgold(thres);
		
		shouldRefreshThresholdList = true;
		Log.i("WeeklyThresholdDialog", "weekly threshold saved...");
		Toast.makeText(_context, "Weekly threshold saved !!!", 3).show();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("WeeklyThresholdDialog", "Creating the dialog...");
		super.onCreate(savedInstanceState);

		db = TelecomCost.getDatabaseManager();
		setContentView(R.layout.weekly_threshold_dialog);

		setTitle("Daily Threshold");

		txtWeek1 = (EditText) findViewById(R.id.txtWeek1);
		txtWeek2 = (EditText) findViewById(R.id.txtWeek2);
		txtWeek3 = (EditText) findViewById(R.id.txtWeek3);
		txtWeek4 = (EditText) findViewById(R.id.txtWeek4);
		
		btnClose = (Button) findViewById(R.id.btnClose);
		btnSave = (Button) findViewById(R.id.btnSave);

		btnClose
				.setOnClickListener(new View.OnClickListener() {

					public void onClick(View arg0) {
						closeDialog();
					}
				});

		btnSave.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				try {
					saveWeeklyThreshold();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		Log.i("WeeklyThresholdDialog", "Dialog ohne bundle created....");
	}

	protected void closeDialog() {
		if (shouldRefreshThresholdList) {
			((WeeklyThresholdListActivity) _context).bindData();
		}
		
		hide();
	}

	@Override
	public void show() {
		Log.i("WeeklyThresholdDialog", "showing the dialog...");
		super.show();
		Log.i("WeeklyThresholdDialog", "dialog on screen...");
		bindWeeklyTreshold();
	}

	@Override
	protected void onStart() {
		Log.i("WeeklyThresholdDialog", "Starting the dialog...");
		super.onStart();
		Log.i("WeeklyThresholdDialog", "dialog started...");
	}

	private void bindWeeklyTreshold() {
		Log.i("WeeklyThresholdDialog", "binding WeeklyThresholdDialog...");

		WeeklyThresholdListActivity activity = (WeeklyThresholdListActivity) _context;
		ArrayList<WeeklyThreshold> thresholds = activity.getThresholds();

		WeeklyThreshold threshold = db.getWeeklyThreshold(MonthlyThreshold.PROP_WEEK1,activity.getMonthlyThreshold().getMonth().getMonthOfYear(),
				thresholds);
		txtWeek1.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getWeeklyThreshold(MonthlyThreshold.PROP_WEEK2,activity.getMonthlyThreshold().getMonth().getMonthOfYear(),
				thresholds);
		txtWeek2.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));
		
		threshold = db.getWeeklyThreshold(MonthlyThreshold.PROP_WEEK3,activity.getMonthlyThreshold().getMonth().getMonthOfYear(),
				thresholds);
		txtWeek3.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));
		
		threshold = db.getWeeklyThreshold(MonthlyThreshold.PROP_WEEK4,activity.getMonthlyThreshold().getMonth().getMonthOfYear(),
				thresholds);
		txtWeek4.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));
	}

	@Override
	protected void onStop() {
		Log.i("WeeklyThresholdDialog", "Stopping the dialog...");
		super.onStop();
		Log.i("WeeklyThresholdDialog", "dialog stopped...");
	}
}
